package com.gec.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gec.model.system.ITripItinerary;
import com.gec.model.system.ITripRole;
import com.gec.model.system.ITripUserRole;
import com.gec.model.vo.AssginRoleVo;
import com.gec.model.vo.ITripRoleQueryVo;
import com.gec.system.mapper.ITripRoleMapper;
import com.gec.system.mapper.ITripUserRoleMapper;
import com.gec.system.service.ITripRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 角色 服务实现类
 * </p>
 *
 * @author chenzhiling
 * @since 2024-01-20
 */
@Service
public class ITripRoleServiceImpl extends ServiceImpl<ITripRoleMapper, ITripRole> implements ITripRoleService {

    @Override
    public IPage<ITripRole> selectPage(IPage<ITripRole> page1, ITripRoleQueryVo iTripRoleQueryVo) {
        IPage<ITripRole> iPage = this.baseMapper.selectPage(page1,iTripRoleQueryVo,null);
        return iPage;
    }

    @Autowired
    private ITripUserRoleMapper iTripUserRoleMapper;

    @Override
    public Map<String, Object> getRolesByUserId(Long userId) {
        //获取所有角色
        List<ITripRole> roles = this.baseMapper.selectList(null);
        //根据用户id查询
        QueryWrapper<ITripUserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        //获取用户已分配的角色
        List<ITripUserRole> userRoles = iTripUserRoleMapper.selectList(queryWrapper);
        //获取用户已分配的角色id
        List<Long> userRoleIds = new ArrayList<>();
        for (ITripUserRole userRole : userRoles) {
            userRoleIds.add(userRole.getRoleId());
        }
        //创建返回的Map
        Map<String, Object> returnMap = new HashMap<>();
        returnMap.put("allRoles",roles);
        returnMap.put("userRoleIds",userRoleIds);
        return returnMap;
    }

    @Override
    public void doAssign(AssginRoleVo assginRoleVo) {
        //根据用户id删除原来分配的角色
        QueryWrapper<ITripUserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",assginRoleVo.getUserId());
        iTripUserRoleMapper.delete(queryWrapper);
        //获取所有的角色id
        List<Long> roleIdList = assginRoleVo.getRoleIdList();
        for (Long roleId : roleIdList) {
            if(roleId != null){
                ITripUserRole sysUserRole = new ITripUserRole();
                sysUserRole.setUserId(assginRoleVo.getUserId());
                sysUserRole.setRoleId(roleId);
                //保存
                iTripUserRoleMapper.insert(sysUserRole);
            }
        }
    }

    @Override
    public ITripRole selectByName(String roleName) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("role_name",roleName);//相当于where typeNo=1
        List<ITripRole> list = baseMapper.selectList(wrapper);
        System.out.println(list);
        return list.size()==0 ?null :list.get(0);
    }
}
